package com.beehoo.dcportal.admin.service.impl;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.beehoo.dcportal.admin.dao.MessageDao;
import com.beehoo.dcportal.admin.service.MessageService;
import com.beehoo.dcportal.common.Constant;
import com.beehoo.dcportal.common.WriteFile;
import com.beehoo.dcportal.common.base.BaseException;
import com.beehoo.dcportal.common.base.BaseService;
import com.beehoo.dcportal.model.FileBean;
import com.beehoo.dcportal.model.MessageBean;
import com.beehoo.dcportal.model.RejectBean;
import com.beehoo.dcportal.utils.CommonUtils;

public class MessageServiceImpl extends BaseService implements MessageService {

	private MessageDao messageDao;

	public void setMessageDao(MessageDao messageDao) {
		this.messageDao = messageDao;
	}

	@Override
	public String createMessage(MessageBean messageBean) throws BaseException {
		validateMessageBean(messageBean);
		messageBean = this.disposeUploadCoverFile(messageBean);
		return messageDao.createMessage(messageBean);
	}

	@Override
	public List<MessageBean> queryMessageForList(String streetId,
			String msgState,String type, String subType, 
			String beginDate, String endDate, int pageNo,int pageRows) throws BaseException {
		if (CommonUtils.nvlToString(streetId).equals("")) {
			throw new BaseException("custom.messages.error.street.id.empty");
		}
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("streetId", streetId);
		paramMap.put("msgState", msgState);
		paramMap.put("type", type);
		paramMap.put("subType", subType);
		paramMap.put("beginDate", beginDate + " 00:00:00");
		paramMap.put("endDate", endDate + " 23:59:59");
		paramMap.put("offset", (pageNo-1)*pageRows);
		paramMap.put("pageRows", pageRows);
		return messageDao.queryMessageForList(paramMap);
	}

	@Override
	public int queryMessageCount(String streetId,
			String msgState,String type, String subType, String beginDate, String endDate)
			throws BaseException {
		if (CommonUtils.nvlToString(streetId).equals("")) {
			throw new BaseException("custom.messages.error.street.id.empty");
		}
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("streetId", streetId);
		paramMap.put("msgState", msgState);
		paramMap.put("type", type);
		paramMap.put("subType", subType);
		paramMap.put("beginDate", beginDate + " 00:00:00");
		paramMap.put("endDate", endDate + " 23:59:59");
		return messageDao.queryMessageCount(paramMap);
	}

	@Override
	public MessageBean queryMessageDetail(String streetId, String msgId,
			String msgState) throws BaseException {
		if (CommonUtils.nvlToString(streetId).equals("")) {
			throw new BaseException("custom.messages.error.street.id.empty");
		}
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("streetId", streetId);
		paramMap.put("msgId", msgId);
		paramMap.put("msgState", msgState);
		return messageDao.queryMessageDetail(paramMap);
	}

	@Override
	public void updateMessage(MessageBean messageBean) throws BaseException {
		validateMessageBean(messageBean);
		if (CommonUtils.nvlToString(messageBean.getMsgId()).equals("")) {
			throw new BaseException("custom.messages.error.message.id.empty");
		}
		messageBean = this.disposeUploadCoverFile(messageBean);
		int ret = messageDao.updateMessage(messageBean);
		if (ret <= 0) {
			throw new BaseException(
					"custom.messages.error.message.update.error");
		}
	}

	@Override
	public void delMessage(String msgId) throws BaseException {
		if (CommonUtils.nvlToString(msgId).equals("")) {
			throw new BaseException("custom.messages.error.message.id.empty");
		}
		int ret = messageDao.delMessage(msgId);
		if (ret <= 0) {
			throw new BaseException("custom.messages.error.message.del.error");
		}
	}

	@Override
	public void auditMessage(MessageBean messageBean, String reason)
			throws BaseException {
		validateAudit(messageBean);
		int ret = messageDao.auditMessage(messageBean);
		if(ret > 0){
			if(messageBean.getMsgState().equals("2")){
				RejectBean rejectBean = new RejectBean();
				rejectBean.setMsgId(messageBean.getMsgId());
				rejectBean.setStreetId(messageBean.getStreetId());
				rejectBean.setRejPid(messageBean.getAuditorId());
				rejectBean.setRejPerson(messageBean.getAuditor());
				rejectBean.setRejReason(reason);
				messageDao.createReject(rejectBean);
			}
		}else{
			throw new BaseException("custom.messages.error.message.audit.error");
		}
	}
	
	@Override
	public String uploadFile(MessageBean messageBean) throws BaseException {
		FileBean fileBean = messageBean.getCoverFile();
		String fileName = ""; 
		if(fileBean != null){
			int fileSize = fileBean.getFileSize();
			if(fileSize > Constant.UPLOAD_FILE_MAX_SIZE){
				throw new BaseException("struts.messages.error.file.too.large");
			}
			fileName = CommonUtils.getUploadFileName(messageBean.getStreetId(), Constant.IMG_MESSAGE_CONTENT);
			File target = new File(messageBean.getContentFilePath() + fileName);
			WriteFile wf = new WriteFile(fileBean.getFile(), target, fileSize);
			fileName = messageBean.getContentFileDir() + fileName;
			try {
				wf.write();
			} catch (BaseException e) {
				throw new BaseException("custom.messages.error.message.upload.error");
			}
		}
		return fileName;
	}
	
	@Override
	public List<RejectBean> queryRejects(String msgId) throws BaseException {
		if (CommonUtils.nvlToString(msgId).equals("")) {
			throw new BaseException("custom.messages.error.message.id.empty");
		}
		return messageDao.queryRejects(msgId);
	}

	private void validateAudit(MessageBean messageBean) throws BaseException {
		if (messageBean == null) {
			throw new BaseException("custom.messages.error.message.audit.error");
		}
		if (CommonUtils.nvlToString(messageBean.getMsgState()).equals("")) {
			throw new BaseException("custom.messages.error.message.state.empty");
		}
		if (CommonUtils.nvlToString(messageBean.getMsgId()).equals("")) {
			throw new BaseException("custom.messages.error.message.id.empty");
		}
		if (CommonUtils.nvlToString(messageBean.getAuditorId()).equals("")) {
			throw new BaseException(
					"custom.messages.error.message.auditor.empty");
		}
		if (CommonUtils.nvlToString(messageBean.getAuditor()).equals("")) {
			throw new BaseException(
					"custom.messages.error.message.auditor.empty");
		}
	}

	private void validateMessageBean(MessageBean messageBean)
			throws BaseException {
		if (messageBean == null) {
			throw new BaseException(
					"custom.messages.error.message.create.error");
		}
		if (CommonUtils.nvlToString(messageBean.getMsgTitle()).equals("")) {
			throw new BaseException("custom.messages.error.message.title.empty");
		}
		if (CommonUtils.nvlToString(messageBean.getMsgType()).equals("")) {
			throw new BaseException(
					"custom.messages.error.message.type.id.empty");
		}
		if (CommonUtils.nvlToString(messageBean.getMsgSource()).equals("")) {
			throw new BaseException(
					"custom.messages.error.message.source.empty");
		}
		if (CommonUtils.nvlToString(messageBean.getDisEnable()).equals("")) {
			throw new BaseException(
					"custom.messages.error.message.dis.enable.empty");
		}
		if (CommonUtils.nvlToString(messageBean.getMsgContent()).equals("")) {
			throw new BaseException(
					"custom.messages.error.message.content.empty");
		}
		if (CommonUtils.nvlToString(messageBean.getMsgState()).equals("")) {
			throw new BaseException("custom.messages.error.message.state.empty");
		}
		if (CommonUtils.nvlToString(messageBean.getStreetId()).equals("")) {
			throw new BaseException("custom.messages.error.street.id.empty");
		}
	}
	
	private MessageBean disposeUploadCoverFile(MessageBean messageBean) throws BaseException{
		FileBean fileBean = messageBean.getCoverFile();
		if(fileBean != null){
			int fileSize = fileBean.getFileSize();
			if(fileSize > Constant.UPLOAD_FILE_MAX_SIZE){
				throw new BaseException("custom.messages.cover.error.file.too.large");
			}
			if(!CommonUtils.validateImageFileType(fileBean.getContentType())){
				throw new BaseException("custom.messages.cover.error.content.type.not.allowed");
			}
			String fileName = CommonUtils.getUploadFileName(messageBean.getStreetId(), Constant.IMG_MESSAGE_COVER);
			messageBean.setMsgCover(messageBean.getCoverFileDir() + fileName);
			
			File target = new File(messageBean.getCoverFilePath()+fileName);
			WriteFile wf = new WriteFile(fileBean.getFile(), target, fileSize);
			try {
				wf.write();
			} catch (BaseException e) {
				throw new BaseException("custom.messages.cover.error.message.upload.error");
			}
		}
                FileBean attachmentFileBean = messageBean.getAttachmentFile();
		if(attachmentFileBean != null){
			int fileSize = attachmentFileBean.getFileSize();
			if(fileSize > Constant.UPLOAD_ATTACHMENT_MAX_SIZE){
				throw new BaseException("custom.messages.attachment.error.file.too.large");
			}
                        String originFileName = attachmentFileBean.getFileName();
                        String originFileNameNoSuffix = originFileName.substring(0,originFileName.lastIndexOf("."));
			String fileName = CommonUtils.getUploadAttachmentName(originFileName.substring(originFileName.lastIndexOf(".")));
			messageBean.setMsgAttachment(messageBean.getAttachmentFileDir() + fileName);
			messageBean.setMsgAttachmentShow(originFileNameNoSuffix);
			File target = new File(messageBean.getAttachmentFilePath()+fileName);
			WriteFile wf = new WriteFile(attachmentFileBean.getFile(), target, fileSize);
			try {
				wf.write();
			} catch (BaseException e) {
				throw new BaseException("custom.messages.attachment.error.message.file.error");
			}
		}
		return messageBean;
	}

}
